$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'quoteFastPrint/quoteprintfactory/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '公司id', name: 'factoryId', index: 'factory_id', width: 80,hidden:true},
            { label: '公司名称', name: 'suppName', index: 'supp_name', width: 80,align:"center"},
            { label: '工厂印刷机名称', name: 'printFactoryName', index: 'print_factory_name', width: 80 ,align:"center"},
            { label: '印刷机id', name: 'printId', index: 'print_id', width: 80 ,hidden:true,align:"center"},
            { label: '印刷机打印类型', name: 'printName', index: 'print_Name', width: 80 ,align:"center"},
            { label: '最小克重', name: 'minWeight', index: 'min_weight', width: 80,align:"center" },
            { label: '最大克重', name: 'maxWeight', index: 'max_weight', width: 80 ,align:"center"},
            { label: '印刷机机器类型', name: 'printType', index: 'print_type', width: 80,align:"center",
                formatter: function (value, options, row) {
                    var valueText = "";
                    var printTypeList = $("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintFactoryEnum;
                    $.each(printTypeList, function (index, module) {
                        if (module.value == value) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
            },
            { label: '是否删除', name: 'delFlag', index: 'del_flag', width: 80,align:"center",
                formatter: function (value, options, row) {
                    return value?"删除":"正常"
                }
            },
            { label: '是否在前端显示', name: 'display', index: 'display', width: 80 ,align:"center",
                formatter: function (value, options, row) {

                    var actions = [];
                    if(value){
                        actions.push('<label class="iSwitch"> <input type="checkbox"  onclick="vm.updatedisplay('+row.id+')" checked> <i></i></label>');
                    }else{
                        actions.push('<label class="iSwitch"> <input type="checkbox" onclick="vm.updatedisplay('+row.id+')"> <i></i></label>');
                    }
                    return actions.join('  ');
                }
            },
            /*{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 },
            { label: '更新时间', name: 'updateDate', index: 'update_date', width: 80 },
            { label: '创建人员', name: 'createUser', index: 'create_user', width: 80 },
            { label: '更新人员', name: 'updateUser', index: 'update_user', width: 80 },
            { label: '', name: 'canUv', index: 'can_uv', width: 80 }*/
            { label: '操作', name: 'option', width: 50 , align: 'center',
                formatter:function(value, options, row){
                    var actions = [];
                    // 配价
                    actions.push('<a href="javascript:void(0);" onclick="return vm.edit(\''+ row.id +'\');">编辑详情</a>') ;
                    return actions.join('  ');
                }
            },
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
		rowList : [25,50,100],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.e.quoteFastPrintColourEnum=$("#jqGrid").jqGrid('getGridParam', 'userData').quoteFastPrintColourEnum;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		quotePrintFactory: {},
        quotePrintFacotoryPirceList:[],
        quoteFastPrintVo:{quoteFastPrint: {},quotePrintFacotoryPirceList:[{display:true}]},
        /* 查询 */
        q:{
            printFactoryName: '',
        },
        e: {
            /* 工厂打印机机器类型 */
            quotePrintFactoryEnum:[],
            /* 工厂打印机打印类型 */
            quotePrintPrintTypeEnum:[],
            /* 快印打印机颜色类型 */
            quoteFastPrintColourEnum:[],
        },
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            <!--快印机器包装类封装数据-->
            vm.quoteFastPrintVo = {quoteFastPrint:{},quotePrintFacotoryPirceList:[{display:true}]};
            /*新增时默认工厂Id为1*/
            vm.quoteFastPrintVo.quoteFastPrint.factoryId=1;
            vm.showList = false;
            vm.title = "新增";
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.quoteFastPrintVo.quoteFastPrint.id == null ? "quoteFastPrint/quoteprintfactory/saveWithPrice" : "quoteFastPrint/quoteprintfactory/updateWithPrice";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.quoteFastPrintVo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "quoteFastPrint/quoteprintfactory/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},edit: function (id) {
            vm.showList = false;
            vm.title = "配价";
            vm.getInfo(id)
        },
		getInfo: function(id){
			$.get(baseURL + "quoteFastPrint/quoteprintfactory/info/"+id, function(r){
                vm.quoteFastPrintVo=r.quoteFastPrintVo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'printFactoryName': vm.q.printFactoryName.trim(),
                },
                page:page
            }).trigger("reloadGrid");
		},reset: function (event) {
            vm.q.printFactoryName = '';
            vm.reload();
        }, deleteRow(index, rows) {//删除改行
        if(vm.quoteFastPrintVo.quotePrintFacotoryPirceList.length<2){
            alert("只有一行,无法删除");
            return;
        }
            rows.splice(index, 1);
        },
        addRow(tableData,event){
            tableData.push({display:true})
        }, //修改是否显示状态
        updatedisplay(id){
            $.get(baseURL + "quoteFastPrint/quoteprintfactory/updateDisplay/"+id, function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        $("#jqGrid").trigger("reloadGrid");
                    });
                }else{
                    alert(r.msg);
                }
            });
        },
	}
});